﻿#ifndef INTELHEXPARSER_H
#define INTELHEXPARSER_H

#include <QObject>
#include <QByteArray>
#include <QList>
#include <QString>

struct MemBlock {
    quint32 addr;      // 起始地址
    QByteArray data;   // 数据
};

class IntelHexParser
{
public:
    explicit IntelHexParser(const QString &src);

    bool parse();                    // 解析 HEX 文件
    QString errorString() const;        // 获取错误信息
    QList<MemBlock> memory() const;  // 获取内存数据

    // 额外：起始段地址、起始线性地址
    quint32 startSegmentAddr() const;
    quint32 startLinearAddr() const;

    // 新增方法：拼成完整内存映射
    QByteArray toContiguousImage(quint32 &baseAddr, quint8 fill = 0xFF) const;

private:
    QString m_src;
    QString m_error;
    QList<MemBlock> m_memory;

    quint32 m_startSegmentAddr = 0;  // 03类型记录
    quint32 m_startLinearAddr = 0;   // 05类型记录

    void addData(quint32 addr, const QByteArray &data);
};

#endif // INTELHEXPARSER_H
